$(document).ready(function () {
    /*先判断用户是否在线*/
    $.ajax({
        url:interfaceGetState+generateMixed(9),
        type:'post',
        success:function (data) {
            if(data.status==0){
                //已登录
                /*是否实名*/
                realName();
            }else {
                //未登录
                alert("请先登录");
                if(window.sessionStorage){
                    var url=window.location.href;
                    sessionStorage.setItem("prePage",url);
                }
                window.location.href="/f/campusinfo/login"+generateMixed(9);
            }
        }
    });

    $("#region").empty();
    var region,country,paymentMethod,paymentPlace;
    /*地区*/
    $.ajax({
        url:interfaceRegionList+generateMixed(9),
        type:'post',
        success:function(data){
            for(var i=0;i<data.page.length;i++){
                $("#region").append("<option value="+data.page[i].id+">"+data.page[i].region+"</option>");
            }
            countryList(data.page[0].id);
            region=data.page[0].id;
        }
    });
    /*地区改变联动事件（国家）*/
    $("#region").change(function(){
        var id=$("#region").val();
        $("#country").empty();
        countryList(id);
        region=id;
    });
    var address=[];
    function countryList(id) {
        $.ajax({
            url:interfaceCountryList+generateMixed(9),
            type:'post',
            data:{regionid:id},
            success:function (data) {
                address=[];
                for(var i=0;i<data.page.length;i++){
                    $("#country").append("<option optionNumber="+i+" value="+data.page[i].id+">"+data.page[i].country+"</option>");
                    address[i]=data.page[i].unLinement;
                }
                $("#address").html(address[0]);
                country=data.page[0].id;
            }
        })
    }
    /*国家改变，缴费地点联动*/
    $("#country").change(function () {
        var cid=$("#country").val();
        /*判断是第几个option被选中*/
         var selectId = $("#country>option:selected").attr("optionNumber");
         if(address[selectId]==undefined){
             $("#address").html('');
         }else{
             $("#address").html(address[selectId]);
         }
        country=cid;
        paymentPlace=address[selectId];
    });
    /*支付事件*/


    /*出国报名，提交*/
    $("#Graduate-school-exam-btn").click(function () {
        /*判断用户是否在线,线下缴费*/
        $.ajax({
            url:interfaceGetState+generateMixed(9),
            type:'post',
            success:function (data) {
                if(data.status==0){
                    //已登录
                    /*是否实名*/
                    realName();
                    if(real){   //已实名
                        $.ajax({
                            url:interfaceAddAbroad+generateMixed(9),
                            type:'post',
                            data:{regionid:region,countryid:country,paymentMethod:3,paymentPlace:paymentPlace},
                            success:function (data) {
                                // console.log(data);
                                if(data.success){
                                    alert(data.message);
                                    window.location.href="/f/campusinfo/entranceExamination"+generateMixed(9);
                                }else {
                                    alert(data.message);
                                }
                            }
                        })
                    }else{      //未实名
                        alert("未实名，请完善个人信息")
                    }
                }else {
                    //未登录
                    alert("请先登录");
                    if(window.sessionStorage){
                        var url=window.location.href;
                        sessionStorage.setItem("prePage",url);
                    }
                    window.location.href="/f/campusinfo/login"+generateMixed(9);
                }
            }
        });
    })
    //支付宝支付报名
    $("#Graduate-school-zhifubao-btn").click(function () {
        /*判断用户是否在线*/
        $.ajax({
            url:interfaceGetState+generateMixed(9),
            type:'post',
            success:function (data) {
                if(data.status==0){
                    //已登录
                    /*是否实名*/
                    realName();
                    if(real){   //已实名
                        $.ajax({
                            url:interfaceAlipayAddAbroad+generateMixed(9),
                            type:'post',
                            data:{regionid:region,countryid:country,paymentMethod:2,paymentPlace:paymentPlace},
                            success:function (data) {
                                var src = data.campusinfoAbroad
                                if(data.success){
                                    alert(data.message);
                                    window.location.href="/alipay/pay"+"?orderNo="+src.orderCode;
                                }else {
                                    alert(data.message);
                                }
                            }
                        })
                    }else{      //未实名
                        alert("未实名，请完善个人信息")
                    }
                }else {
                    //未登录
                    alert("请先登录");
                    if(window.sessionStorage){
                        var url=window.location.href;
                        sessionStorage.setItem("prePage",url);
                    }
                    window.location.href="/f/campusinfo/login"+generateMixed(9);
                }
            }
        });
    })
    //微信支付报名
    $("#Graduate-school-weixin-btn").click(function () {
        /*判断用户是否在线*/
        $.ajax({
            url:interfaceGetState+generateMixed(9),
            type:'post',
            success:function (data) {
                if(data.status==0){
                    //已登录
                    /*是否实名*/
                    realName();
                    if(real){   //已实名
                        $.ajax({
                            url:interfaceAlipayAddAbroad+generateMixed(9),
                            type:'post',
                            data:{regionid:region,countryid:country,paymentMethod:1,paymentPlace:paymentPlace},
                            success:function (data) {
                                var src = data.campusinfoAbroad
                                if(data.success){
                                    alert(data.message);
                                    // window.location.href="/alipayWeixinw/wxpay/unifiedOrderByPublicNumber"+"?orderNo="+src.orderCode;
                                    window.location.href="/alipayWeixin/pay/order"+"?orderNo="+src.orderCode;
                                }else {
                                    alert(data.message);
                                }
                            }
                        })
                    }else{      //未实名
                        alert("未实名，请完善个人信息")
                    }
                }else {
                    //未登录
                    alert("请先登录");
                    if(window.sessionStorage){
                        var url=window.location.href;
                        sessionStorage.setItem("prePage",url);
                    }
                    window.location.href="/f/campusinfo/login"+generateMixed(9);
                }
            }
        });
    })

})